<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>斜率优化动态规划 学习笔记 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="发掘状态转移方程的性质">
<meta property="og:type" content="article">
<meta property="og:title" content="斜率优化动态规划 学习笔记">
<meta property="og:url" content="http://example.com/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="发掘状态转移方程的性质">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://cdn.luogu.com.cn/upload/image_hosting/7u5hs23p.png">
<meta property="og:image" content="https://cdn.luogu.com.cn/upload/image_hosting/b616l8pz.png">
<meta property="og:image" content="https://img1.imgtp.com/2022/05/29/0SczF5ou.gif">
<meta property="og:image" content="https://cdn.luogu.com.cn/upload/image_hosting/lmflipw8.png">
<meta property="article:published_time" content="2022-05-29T11:00:05.180Z">
<meta property="article:modified_time" content="2023-07-19T11:50:58.363Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.luogu.com.cn/upload/image_hosting/7u5hs23p.png">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-斜率优化动态规划 学习笔记" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="article-date">
  <time class="dt-published" datetime="2022-05-29T11:00:05.180Z" itemprop="datePublished">2022-05-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      斜率优化动态规划 学习笔记
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>发掘状态转移方程的性质</p>
<p>首先看这样一个问题：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P3195">洛谷 P3195 [HNOI2008]玩具装箱</a><br>题目大意:<br>有 $n$ 个物品排成一行,第 $i$ 个物品权值为 $C<em>i$ ,现要求将这些物品分成若干段,每段的花费为 $((\sum</em>{i=l}^{r}{C_i})-L)^2$ (其中 $l$,$r$ 为这一段的左右端点, $L$ 为给定常数),问最小的总花费.保证 $1 \leq n \leq 5 \times 10^4$，$1 \leq L \leq 10^7$，$1 \leq C_i \leq 10^7$ .</p>
</blockquote>
<p>这题显然是一道 DP 题. 令 $dp<em>{i}$ 为考虑前 $i$ 个物品的最小代价, $sum_i=\sum</em>{j=1}^{i}C_j$ , 可以想出这样的状态转移方程:</p>
<script type="math/tex; mode=display">dp_i=\min_{0\le j<i}\{dp_j+(sum_i-sum_{j}-L)^2\}</script><p>然而, 如果暴力转移时间复杂度是 $\Theta(n^2)$ 的, 显然会T, 有什么办法优化吗?</p>
<p>动态规划优化的一个重要思路是把可能决策的范围缩小 (即排除不可能的决策) . 顺着这个思路试试看?</p>
<p>首先我们观察状态转移方程, 发现这个 $\min$ 函数很碍事, 把它去掉:</p>
<script type="math/tex; mode=display">dp_i=dp_j+(sum_i-sum_{j}-L)^2</script><p>这个方程太长了, 所以考虑令 $a_i=sum_i-L$ , $b_i=sum_i$ , 代入得:</p>
<script type="math/tex; mode=display">dp_i=dp_j+(a_i-b_j)^2</script><p>这个平方也很碍事, 把它展开:</p>
<script type="math/tex; mode=display">dp_i=dp_j+{a_i}^2-2a_ib_j+{b_j}^2</script><p>把跟 $i$ 有关的移到等号一边, 跟 $j$ <del>(读作”勾”)</del> 有关的放到另一边:</p>
<script type="math/tex; mode=display">2a_ib_j+dp_i-{a_i}^2=dp_j+{b_j}^2</script><p>……感觉直到现在我们做的都是一些很常规的操作…… 但是如果我们放一个直线的表达式跟它对比一下?</p>
<div class="table-container">
<table>
<thead>
<tr>
<th>$(2a_i)b_j+(dp_i-{a_i}^2)=(dp_j+{b_j}^2)$</th>
</tr>
</thead>
<tbody>
<tr>
<td>$kx+b=y$</td>
</tr>
</tbody>
</table>
</div>
<p>于是我们惊奇地发现, <strong>状态转移方程竟然可以看作一条斜率为 $2a_i$ , 截距为 $dp_i-{a_i}^2$ 的直线和一个在 $(b_j,dp_j+{b_j}^2)$ 的点(我管它叫决策点)!</strong></p>
<p>所以, “转移” 这个过程就可以理解为找到一个斜率为 $2a_i$ 的直线交于一个已有的决策点. </p>
<p><img src="https://cdn.luogu.com.cn/upload/image_hosting/7u5hs23p.png" alt=""></p>
<p>其中, 红色直线表示 $dp_i$ 对应的直线,绿色直线表示一个可能的 $dp_j$ 对应的直线, 点 A 表示这个 $dp_j$ 对应的决策点, 点 B 表示 $dp_i$ 对应的决策点.</p>
<p>所以我们怎么找一条最优的直线, 使得 $dp_i$ 最小? 难道要枚举 $j$ 吗?</p>
<p>当然不用. 因为 $dp_i-{a_i}^2$ 只与 $i$ 有关, 所以只要这条直线的截距最小, $dp_i$ 就是最小的. 我们假设平面上已经有一堆可供转移的决策点和直线:</p>
<p><img src="https://cdn.luogu.com.cn/upload/image_hosting/b616l8pz.png" alt=""></p>
<p>可以想象 $dp_i$ 对应的直线从下往上移动 (别忘了这条直线的斜率不变) , 显然它第一个接触到的点就是最优决策点.</p>
<p><img src="https://img1.imgtp.com/2022/05/29/0SczF5ou.gif" alt=""></p>
<p>如果您学过计算几何 (我没学过QAQ) , 您可以马上发现<strong>潜在的最优决策点都在这一堆点的下凸包上</strong>!</p>
<p><img src="https://cdn.luogu.com.cn/upload/image_hosting/lmflipw8.png" alt=""></p>
<p>所以我们只需要用一个单调队列来维护这个凸包就可以了.</p>
<p>具体怎么维护等到以后再写罢, 博主累了.</p>
<p>综上所述, 我们通过发掘状态转移方程的性质做到了摊还 $\Theta(1)$ 的转移, 非常优秀.</p>
<p>代码:</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> int ll</span></span><br><span class="line"><span class="type">const</span> <span class="type">int</span> MAXN=<span class="number">50000</span>;</span><br><span class="line"><span class="type">int</span> n,L,c[MAXN+<span class="number">5</span>],sum[MAXN+<span class="number">5</span>],dp[MAXN+<span class="number">5</span>];</span><br><span class="line">deque&lt;<span class="type">int</span>&gt; q;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">a</span><span class="params">(<span class="type">int</span> i)</span></span>&#123;<span class="keyword">return</span> sum[i]+i;&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">b</span><span class="params">(<span class="type">int</span> i)</span></span>&#123;<span class="keyword">return</span> sum[i]+i+L+<span class="number">1</span>;&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">X</span><span class="params">(<span class="type">int</span> i)</span></span>&#123;<span class="keyword">return</span> <span class="built_in">b</span>(i);&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">Y</span><span class="params">(<span class="type">int</span> i)</span></span>&#123;<span class="keyword">return</span> (dp[i])+(<span class="built_in">b</span>(i)*<span class="built_in">b</span>(i));&#125;</span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">slope</span><span class="params">(<span class="type">int</span> i,<span class="type">int</span> j)</span></span>&#123;<span class="keyword">return</span> <span class="built_in">double</span>(<span class="built_in">Y</span>(i)-<span class="built_in">Y</span>(j))/<span class="built_in">double</span>(<span class="built_in">X</span>(i)-<span class="built_in">X</span>(j));&#125;</span><br><span class="line"><span class="function"><span class="type">signed</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    ios::<span class="built_in">sync_with_stdio</span>(<span class="literal">false</span>);</span><br><span class="line">    cin&gt;&gt;n&gt;&gt;L;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        cin&gt;&gt;c[i];sum[i]=sum[i<span class="number">-1</span>]+c[i];</span><br><span class="line">    &#125;</span><br><span class="line">    q.<span class="built_in">push_back</span>(<span class="number">0</span>);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        <span class="type">double</span> qwq=<span class="number">114514.1919810</span>;</span><br><span class="line">        <span class="keyword">if</span>(q.<span class="built_in">size</span>()&gt;=<span class="number">2</span>)qwq=<span class="built_in">slope</span>(q[<span class="number">0</span>],q[<span class="number">1</span>]);</span><br><span class="line">        <span class="keyword">while</span>(q.<span class="built_in">size</span>()&gt;=<span class="number">2</span>&amp;&amp;<span class="built_in">slope</span>(q[<span class="number">0</span>],q[<span class="number">1</span>])&lt;<span class="built_in">double</span>(<span class="number">2</span>*<span class="built_in">a</span>(i)))&#123;</span><br><span class="line">            q.<span class="built_in">pop_front</span>();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">int</span> j=q.<span class="built_in">front</span>();</span><br><span class="line">        dp[i]=dp[j]+(<span class="built_in">a</span>(i)-<span class="built_in">b</span>(j))*(<span class="built_in">a</span>(i)-<span class="built_in">b</span>(j));</span><br><span class="line">        <span class="keyword">while</span>(q.<span class="built_in">size</span>()&gt;=<span class="number">2</span>&amp;&amp;<span class="built_in">slope</span>(q[q.<span class="built_in">size</span>()<span class="number">-1</span>],q[q.<span class="built_in">size</span>()<span class="number">-2</span>])&gt;<span class="built_in">slope</span>(q[q.<span class="built_in">size</span>()<span class="number">-2</span>],i))q.<span class="built_in">pop_back</span>();</span><br><span class="line">        q.<span class="built_in">push_back</span>(i);</span><br><span class="line">    &#125;</span><br><span class="line">    cout&lt;&lt;dp[n]&lt;&lt;endl;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" data-id="clk9o8ln7000d8wi5dk63hzlx" data-title="斜率优化动态规划 学习笔记" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          CSP-S 2022 游记
        
      </div>
    </a>
  
  
    <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">PKUSC一句话游记</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>